package gui;


import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

public class SellPage extends JPanel {
	private static final long serialVersionUID = 744949355054204243L;

	/**
	 * This is the SellPage. The page where users manage their sales
	 * Users can see information that is specific to them.
	 * 
	 */
	JPanel lowerPanel, sellPanel, titlePanel, post_bookPanel;

	/* titlePanel Components */
	JLabel title;

	/* adminPanel Components */
	JLabel bookName, author, edition, quality, ISBN, price;
	JTextField bookNameField, authorField, editionField, qualityField,
		ISBNField, priceField;
	JButton post_bookButton, returnButton, logoutButton;

	/* searchPanel Components */
	SearchPanel4 searchPanel;

	// Parent Panel
	GUI guiObject;
	
	public void clearFields()
	{
		bookNameField.setText("");
		authorField.setText("");
		editionField.setText("");
		qualityField.setText("");
		ISBNField.setText("");
		priceField.setText("");
		searchPanel.clearFields();
	}

	public SellPage(GUI guiObject) {
		setLayout(new GridBagLayout());

		this.guiObject = guiObject;

		createTitlePanel();
		createLowerPanel();

		Constraints.setConstraints(0,0,1,1,1,1,GridBagConstraints.NORTH,
				GridBagConstraints.BOTH, new Insets(0,0,0,0),0,0);

		add(titlePanel, Constraints.gbc);

		Constraints.setConstraints(0,1,1,1,1,1,GridBagConstraints.SOUTH,
				GridBagConstraints.BOTH, new Insets(5,0,5,0),0,0);

		add(new JSeparator(SwingConstants.HORIZONTAL), Constraints.gbc);

		Constraints.setConstraints(0,1,1,1,1,1,GridBagConstraints.NORTH,
				GridBagConstraints.NONE, new Insets(5,0,0,5),0,0);

		add(lowerPanel, Constraints.gbc);

		return;
	}

	private void createTitlePanel() {
		titlePanel = new JPanel(new GridBagLayout());

		title = new JLabel("WELCOME SELLER");
		title.setForeground(Color.white);
		titlePanel.setBackground(Color.yellow);
		title.setFont(new Font("Serif", Font.BOLD, 48));

		Constraints.setConstraints(0,0,1,1,1,1,GridBagConstraints.CENTER,
				GridBagConstraints.BOTH,new Insets(5,5,5,5),0,0);

		titlePanel.add(title, Constraints.gbc);
	}

	private void createLowerPanel() {
		lowerPanel = new JPanel(new GridBagLayout());
		lowerPanel.setVisible(true);

		createSellPanel();
		searchPanel = new SearchPanel4(guiObject, this);

		Constraints.setConstraints(0,0,1,1,0,0,GridBagConstraints.NORTHEAST,
				GridBagConstraints.NONE,new Insets(0,0,0,0),0,0);

		lowerPanel.add(searchPanel, Constraints.gbc);

		Constraints.setConstraints(1,0,1,1,1,1,GridBagConstraints.EAST,
				GridBagConstraints.BOTH,new Insets(5,5,5,5),0,0);

		lowerPanel.add(new JSeparator(SwingConstants.VERTICAL), Constraints.gbc);

		Constraints.setConstraints(2,0,1,1,0,0,GridBagConstraints.NORTHEAST,
				GridBagConstraints.NONE,new Insets(0,0,0,0),0,0);

		lowerPanel.add(sellPanel, Constraints.gbc);
	}

	private void createSellPanel() {

		sellPanel = new JPanel(new GridBagLayout());
		post_bookPanel = new JPanel(new GridBagLayout());

		Constraints.setConstraints(0,1,1,1,1,1,GridBagConstraints.WEST,
				GridBagConstraints.BOTH, new Insets(5,5,5,5),0,0);

		JPanel buttonPanel = new JPanel(new GridBagLayout());

		/*--ADDING POST BOOK PANEL--*/

		//Making a border then making a title around it.
		Border loweredetched = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
		TitledBorder title = BorderFactory.createTitledBorder(loweredetched, "Post Book");
		title.setTitleJustification(TitledBorder.RIGHT);
		post_bookPanel.setBorder(title);

		bookName = new JLabel("Name", SwingConstants.RIGHT);
		bookNameField = new JTextField();
		bookNameField.setPreferredSize(new Dimension(200, 20));
		Constraints.setConstraints(0,0,1,1,1,1,GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(0,0,0,5),0,0);
		post_bookPanel.add(bookName, Constraints.gbc);
		Constraints.setConstraints(1,0,1,1,1,1,GridBagConstraints.CENTER,
				GridBagConstraints.NONE, new Insets(0,0,0,0),0,0);
		post_bookPanel.add(bookNameField, Constraints.gbc);

		author = new JLabel("Author", JLabel.RIGHT);
		authorField = new JTextField();
		authorField.setPreferredSize(new Dimension(200, 20));
		Constraints.setConstraints(0,1,1,1,1,1,GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(0,0,0,5),0,0);
		post_bookPanel.add(author, Constraints.gbc);
		Constraints.setConstraints(1,1,1,1,1,1,GridBagConstraints.CENTER,
				GridBagConstraints.NONE, new Insets(0,0,0,0),0,0);
		post_bookPanel.add(authorField, Constraints.gbc);

		edition = new JLabel("Edition", SwingConstants.RIGHT);
		editionField = new JTextField();
		editionField.setPreferredSize(new Dimension(200, 20));
		Constraints.setConstraints(0,2,1,1,1,1,GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(0,0,0,5),0,0);
		post_bookPanel.add(edition, Constraints.gbc);
		Constraints.setConstraints(1,2,1,1,1,1,GridBagConstraints.CENTER,
				GridBagConstraints.NONE, new Insets(0,0,0,0),0,0);
		post_bookPanel.add(editionField, Constraints.gbc);
		
		quality = new JLabel("Quality", SwingConstants.RIGHT);
		qualityField = new JTextField();
		qualityField.setPreferredSize(new Dimension(200, 20));
		Constraints.setConstraints(0,3,1,1,1,1,GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(0,0,0,5),0,0);
		post_bookPanel.add(quality, Constraints.gbc);
		Constraints.setConstraints(1,3,1,1,1,1,GridBagConstraints.CENTER,
				GridBagConstraints.NONE, new Insets(0,0,0,0),0,0);
		post_bookPanel.add(qualityField, Constraints.gbc);
		
		ISBN = new JLabel("ISBN", SwingConstants.RIGHT);
		ISBNField = new JTextField();
		ISBNField.setPreferredSize(new Dimension(200, 20));
		Constraints.setConstraints(0,4,1,1,1,1,GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(0,0,0,5),0,0);
		post_bookPanel.add(ISBN, Constraints.gbc);
		Constraints.setConstraints(1,4,1,1,1,1,GridBagConstraints.CENTER,
				GridBagConstraints.NONE, new Insets(0,0,0,0),0,0);
		post_bookPanel.add(ISBNField, Constraints.gbc);
		
		price = new JLabel("Price", SwingConstants.RIGHT);
		priceField = new JTextField();
		priceField.setPreferredSize(new Dimension(200, 20));
		Constraints.setConstraints(0,5,1,1,1,1,GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(0,0,0,5),0,0);
		post_bookPanel.add(price, Constraints.gbc);
		Constraints.setConstraints(1,5,1,1,1,1,GridBagConstraints.CENTER,
				GridBagConstraints.NONE, new Insets(0,0,0,0),0,0);
		post_bookPanel.add(priceField, Constraints.gbc);

		// Buttons
		post_bookButton = new JButton("Post Book");
		post_bookButton.setActionCommand("selladd");
		post_bookButton.addActionListener(guiObject);
		Constraints.setConstraints(1,6,1,1,1,1,GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(0,50,0,3),0,0);
		post_bookPanel.add(post_bookButton, Constraints.gbc);

		/*--ADDING nameRegisterPanel TO userPanel--*/
		Constraints.setConstraints(0,3,2,2,1,1,GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(0,50,0,0),0,0);
		sellPanel.add(post_bookPanel, Constraints.gbc);
		
		returnButton = new JButton("Back");
		returnButton.setActionCommand("backtouserpage");
		returnButton.addActionListener(guiObject);
		Constraints.setConstraints(1,0,1,1,1,1,GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(5,0,0,194),0,0);
		buttonPanel.add(returnButton, Constraints.gbc);

		logoutButton = new JButton("Logout");
		logoutButton.setActionCommand("logout");
		logoutButton.addActionListener(guiObject);
		Constraints.setConstraints(1,0,1,1,1,1,GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(5,0,0,0),0,0);
		buttonPanel.add(logoutButton, Constraints.gbc);

		Constraints.setConstraints(0,0,1,1,1,1,GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(5,0,5,3),0,0);
		sellPanel.add(buttonPanel, Constraints.gbc);
	}
}